package cn.zzp.product.service.impl;

import cn.zzp.basic.exception.BusinessException;
import cn.zzp.basic.service.impl.BaseServiceImpl;
import cn.zzp.basic.util.BaiduAuditUtils;
import cn.zzp.product.domain.Product;

import cn.zzp.product.domain.ProductDetail;
import cn.zzp.product.domain.ProductOnlineAuditLog;
import cn.zzp.product.mapper.ProductDetailMapper;
import cn.zzp.product.mapper.ProductMapper;
import cn.zzp.product.mapper.ProductOnlineAuditLogMapper;
import cn.zzp.product.service.ProductService;
import cn.zzp.product.domain.Product;
import cn.zzp.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * @ClassName
 * @Description
 * @Author 祝志鹏
 * @Date 2022/7/14 14:16
 */
@Service
//@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ProductServiceImpl extends BaseServiceImpl<Product> implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    /**
     * 上架
     * @param ids
     */
    @Transactional
    @Override
    public void onsale(List<Long> ids) {
        Date date = new Date();
        productMapper.onsale(ids, date);
    }

    @Override
    public void offsale(List<Long> ids) {
        Date date = new Date();
        productMapper.offsale(ids, date);
    }

    /**
     * 新增
     * @param product
     */
    @Transactional
    @Override
    public void add(Product product) {
        // 审核
        Boolean aBoolean = BaiduAuditUtils.TextCensor(product.getName());
        if (!aBoolean) {
            throw new BusinessException("商品名称违规，请重新输入!!!");

        }

        super.add(product);
        // 添加日志
        ProductOnlineAuditLog log = new ProductOnlineAuditLog();

        log.setProduct_id(product.getId());
        log.setState(1);
        log.setNote("审核成功");
        productOnlineAuditLogMapper.add(log);
        // 添加详情
        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProduct_id(product.getId());
        productDetailMapper.add(productDetail);
    }

    @Transactional
    @Override
    public void update(Product product) {
        // 审核
        Boolean aBoolean = BaiduAuditUtils.TextCensor(product.getName());
        if (!aBoolean) {
            throw new BusinessException("商品名称违规，请重新输入!!!");
        }

        super.update(product);

        ProductDetail productDetail = product.getProductDetail();
        productDetail.setProduct_id(product.getId());
        productDetailMapper.update(productDetail);
    }
}
